﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Threading;

namespace HubbleTelescope.Framework
{
    /// <summary>
    /// Quick helper class to throw stuff on the UI thread
    /// </summary>
    public static class Execute
    {
#if WINDOWS_PHONE
        public static Dispatcher Dispatcher
        {
            get;
            set;
        }
#endif

        /// <summary>
        /// Raises an action on the UI thread if needed
        /// </summary>
        /// <param name="action">Action to invoke</param>
        public static void OnUIThread(Action action)
        {
#if WINDOWS_PHONE
            var dispatcher = Dispatcher;
#else
            var dispatcher = Application.Current.Dispatcher;
#endif
            if (dispatcher.CheckAccess())
            {
                action();
            }
            else
            {
                dispatcher.BeginInvoke(action);
            }
        }
    }
}
